import ArraySort.*;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Random;
import java.util.Scanner;

public class Test {


    //类的展示字符串函数
    public <T> void display(T[] arr,PrintWriter outputStream) {
        for (T a : arr) {
            System.out.println(a.toString() + " ");//将输出显示到屏幕
            outputStream.println(a.toString() + " ");//将输出写出到文本文件
        }
    }

    //展示int函数
    public void display(int[] arr) {
        for (int a : arr) {
            System.out.println(a + " ");
        }
    }
    //主函数
    public static void main(String[] args) {

        PrintWriter outputStream= null;
        System.out.print("Enter file name:");
        Scanner scanner = new Scanner(System.in);
        String fileName = scanner.nextLine();
        try
        {
            //将文件交给输出流
            outputStream = new PrintWriter(fileName);
        }
        catch(FileNotFoundException e)
        {
            //报错处理
            System.out.println("Error opening the file"+ fileName);
        }

        Test b = new Test();
        MergeSort test = new MergeSort();
        Student[] stu = new Student[5];
        Clerk[] clk = new Clerk[5];
        Random rand = new Random();
        Integer id[] = new Integer[5];
        for (int i = 0; i < 5; i++) {
            id[i] = rand.nextInt(1000);
        }
        String stuName[] = {"Alex", "Celler", "Becall", "Ewag", "Dike"};
        String clkName[] = {"Creen", "Ded", "Ahite", "Bellow", "Eink"};
        for (int i = 0; i < 5; i++) {
            stu[i] = new Student(id[i], stuName[i]);
            clk[i] = new Clerk(clkName[i]);
        }
        System.out.println("The Clerk before sorting: ");//将输出显示到屏幕
        outputStream.println("The Clerk before sorting: ");//将输出写出到文本文件
        b.display(clk,outputStream);
        System.out.println();//将输出显示到屏幕
        outputStream.println();//将输出写出到文本文件

        test.mergeSort(clk);   //第一种参数
        System.out.println("The Clerk after sorting: ");//将输出显示到屏幕
        outputStream.println("The Clerk after sorting: ");//将输出写出到文本文件
        b.display(clk,outputStream);
        System.out.println("-------------------------------------------------");//将输出显示到屏幕
        outputStream.println("-------------------------------------------------");//将输出写出到文本文件



        System.out.println("The Student before sorting: ");//将输出显示到屏幕
        outputStream.println("The Student before sorting: ");//将输出写出到文本文件
        b.display(clk,outputStream);
        System.out.println();//将输出显示到屏幕
        outputStream.println();//将输出写出到文本文件
        test.mergeSort(stu, new Comparator<Comparable>() {//第二种参数
            @Override
            public int compare(Comparable o1, Comparable o2) {
                return ((Student) o1).getId().compareTo(((Student) o2).getId());
            }
        });
        System.out.println("The Student after sorting: ");//将输出显示到屏幕
        outputStream.println("The Student after sorting: ");//将输出写出到文本文件
        b.display(clk,outputStream);
        outputStream.close();//关闭文件输出流
    }
}
